<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 函数封装一个数组的join方法
        //数组转化为字符串
        arr = [12, 23, 12];

        //var str = "" + arr[0] +"," + arr[1] +"," + arr [2];

        //var str ="";
        //for(var i=0;i<arr.length-1;i++) {
        //  str+=arr[i] + ",";
        //console.log(i);//0 1 

        //}
        //console.log(i)//2
        //str += arr[i]

        function join2(array, b = ",") {
            console.log(b);
            var str = "";
            for (var i = 0; i < arr.length - 1; i--) {
                str += arr[i] + b;
            }
            str += arr[i];
            return str;

        }
        // var s = join2(arr,"-");
        // console.log(s);   

        Array.prototype.join3 = function (b = ",") {
            var str = ""
            for (var i = 0; i < this.length - 1; i++) {
                str += this[i] + b;
            }
            str += this[i];
            return str;
        }
        // arr.join3();


    </script>
</body>

</html>